package com.crealth;

import org.apache.log4j.Logger;
import com.crealth.manager.MybatisManager;
import com.crealth.memcached.MemcachedMgr;
import com.crealth.utils.Utils;

public class UpdateProductUserIDMain {
	private static Logger logger = Logger.getLogger("running_log");
	private static Logger errorLog = Logger.getLogger("error_log");

	public static void main(String[] args) {
		try {
			String osName = Utils.getOSName();
			logger.info("OS: " + osName);
			Config.initConfig();
			MybatisManager.initSessionFactory();
		} catch (Exception e) {
			errorLog.error("init config error", e);
			System.exit(0);
		}

		ConstantDefine.initConstant();
		MemcachedMgr.initMemcached();
		/*
		 * List<ConsumerProductModel> productls =
		 * ConsumerProductDb.selectAllConsumerProduct(); for
		 * (ConsumerProductModel product : productls) { int id =
		 * product.getId(); int contractID = product.getContractID();
		 * LoanContractModel contract =
		 * LoanContractDb.selectLoanContractByID(contractID); if (contract !=
		 * null) { int userID = contract.getUserID();
		 * ConsumerProductDb.updateUserIDByID(id, userID); } else {
		 * logger.error("contract is null product id: " + id); }
		 * 
		 * }
		 */
		

		Integer res = 0;
		for (int i = -1; i < ConstantDefine.bankls.size(); ++i) {
			String bankName = "";
			if (i >= 0) {
				bankName = ConstantDefine.bankls.get(i);
			}
			String strSql = "UPDATE consumer_car SET installment_bank = '"
					+ bankName + "' WHERE installment_bank = '" + i + "'";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();

			strSql = "UPDATE consumer_credit_card SET credit_card_bank = '"
					+ bankName + "' WHERE credit_card_bank = '" + i + "'";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();

			strSql = "UPDATE consumer_loan SET loan_bank = '" + bankName
					+ "' WHERE loan_bank = '" + i + "' AND loan_type = 1";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();

			strSql = "UPDATE consumer_house SET installment_bank = '"
					+ bankName + "' WHERE installment_bank = '" + i + "'";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();
		}

		for (int i = -1; i < ConstantDefine.pettyBankls.size(); ++i) {
			String bankName = "";
			if (i >= 0) {
				bankName = ConstantDefine.pettyBankls.get(i);
			}
			String strSql = "UPDATE consumer_loan SET loan_bank = '" + bankName
					+ "' WHERE loan_bank = '" + i + "' AND loan_type = 2";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();
		}

		for (int i = 0; i < ConstantDefine.consumerTypels.size(); ++i) {
			String bankName = "";
			if (i >= 0) {
				bankName = ConstantDefine.consumerTypels.get(i);
			}
			String strSql = "UPDATE consumer_info SET cli_type = '" + bankName
					+ "' WHERE cli_type = '" + i + "'";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();
		}

		for (int i = -1; i < ConstantDefine.housePropertyAddrls.size(); ++i) {
			String bankName = "";
			if (i >= 0) {
				bankName = ConstantDefine.housePropertyAddrls.get(i);
			}
			String strSql = "UPDATE consumer_house SET house_addr = '"
					+ bankName + "' WHERE house_addr = '" + i + "'";
			res = MybatisManager.userMapper.updateSql(strSql);
			MybatisManager.sqlSession.commit();
		}
		logger.info("----      res: " + res);
		logger.info("----      finished    ------------");

	}

}
